package com.ruoyi.web.controller.common;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.service.SysRegisterService;
import com.ruoyi.system.coretools.GoogleAuthTools;
import com.ruoyi.system.domain.SysFileInfo;
import com.ruoyi.system.domain.TUserInfo;
import com.ruoyi.system.domain.dto.TUserInfoDto;
import com.ruoyi.system.service.ISysFileInfoService;
import com.ruoyi.system.service.ITUserInfoService;
import com.ruoyi.system.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.security.auth.login.LoginException;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.security.GeneralSecurityException;
import java.util.List;
import java.util.Objects;

import com.ruoyi.framework.web.service.TokenService;

/**
 * google三方登录
 *
 * @author ruoyi
 * @date 2024-04-23
 */
@RestController
@RequestMapping("/system/google")
public class GoogleAuthController extends BaseController {
    @Autowired
    GoogleAuthTools googleAuthTools;
    @Resource
    private TokenService tokenService;
    @Autowired
    UserService userService;

    @Resource
    private ITUserInfoService userInfoService;
    @Autowired
    SysRegisterService sysRegisterService;

    /**
     * google login
     */
    @GetMapping("/googleLogin")
    public AjaxResult googleLogin(@RequestParam(name = "idTokenString", defaultValue = "") String idTokenString) throws Exception {
        logger.info("收到google登录请求，客户端从google那边拿过来的idTokenString:{}", idTokenString);
        /*
        android
        GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                .requestIdToken(getString(R.string.server_client_id))
                .requestEmail()
                .build();*/


        //先验证google那边的信息
        String email = googleAuthTools.googleVerify(idTokenString);

        //再判断在平台是否有账号，有账号直接登录拿平台内通用token，没账号注册登录拿平台内通用token
        TUserInfo result = null;

        TUserInfo info = null;
        if (StringUtils.isNotEmpty(email)) {
            info = userInfoService.selectByEmail(email);
        }
        if (Objects.isNull(info)) {
            //多走个自动注册流程
            sysRegisterService.googleNewUserAutoRegister(email);

        }

        //走自动平台登录流程
        //准备好后把参数传给系统内部的注册接口后登录接口返回token
        TUserInfoDto tUserInfoDto = new TUserInfoDto();
        //tUserInfoDto.setVipId(userId);
        tUserInfoDto.setUserEmail(email);
        //登录
        result = userService.userLoginByGoogle(tUserInfoDto);


        String appToken = tokenService.createAppToken(result);
        //google idTokenString换平台token
        logger.info("结束google登录请求:{}", result);
        return AjaxResult.success(appToken);
    }

}
